package com.liang.blog.reception.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.blog.common.result.Result;
import com.liang.blog.model.BlogArticle;
import com.liang.blog.model.base.BlogPageInfo;
import com.liang.blog.reception.service.BlogArticleService;
import com.liang.blog.vo.article.ArticleInfoVo;
import com.liang.blog.vo.article.BlobCommentsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 博客文章控制层
 * @Author：YanWenLiang
 * @Date:2023-09-20-8:50
 */
@RestController
@RequestMapping("/article")
public class BlogArticleController {

    @Autowired
    private BlogArticleService blogArticleService;

    /**
     * 添加文章
     * @param articleInfoVo
     * @return
     */
    @PostMapping("/inner/addArticle")
    public Result<Boolean> addArticle(@RequestBody ArticleInfoVo articleInfoVo){
        return Result.ok(blogArticleService.addArticle(articleInfoVo));
    }

    /**
     * 添加评论
     * @param blobCommentsVO
     * @return
     */
    @PostMapping("/inner/addArticleComments")
    public Result<Boolean> addArticleComments(@RequestBody BlobCommentsVO blobCommentsVO){
        return Result.ok(blogArticleService.addArticleComments(blobCommentsVO));
    }

    /**
     * 查询评论
     * @param articleId
     * @return
     */
    @GetMapping("/public/getArticleComments/{articleId}")
    public Result<Map<String, Object>> getArticleComments(@PathVariable Long articleId){
        return Result.ok(blogArticleService.getArticleComments(articleId));
    }

    /**
     * 通过 id 获取文章信息
     * @param id
     * @return
     */
    @GetMapping("/public/getArticleById/{id}")
    public Result getArticleById(@PathVariable String id){
        ArticleInfoVo articleInfoVo = blogArticleService.getArticleById(Long.parseLong(id));
        return Result.ok(articleInfoVo);
    }

    /**
     * 获取文章列表
     * @param articleInfoVo
     * @return
     */
    @PostMapping("/public/getArticleList/{page}/{pageSize}")
    public Result<BlogPageInfo<BlogArticle>> getArticleList(@RequestBody ArticleInfoVo articleInfoVo,
                                                            @PathVariable int page,
                                                            @PathVariable int pageSize){
        PageHelper.startPage(page, pageSize);
        List<ArticleInfoVo> articleList = blogArticleService.getArticleList(articleInfoVo);
        BlogPageInfo<BlogArticle> blogPageInfo =
                new BlogPageInfo(new PageInfo(articleList).getTotal(), articleList);
        return Result.ok(blogPageInfo);
    }

    /**
     * 获取文章列表
     * @param articleInfoVo
     * @return
     */
    @PostMapping("/getArticleListByUserId")
    public Result<List<ArticleInfoVo>> getArticleListByUserId(@RequestBody ArticleInfoVo articleInfoVo){
        List<ArticleInfoVo> articleList = blogArticleService.getArticleList(articleInfoVo);
        return Result.ok(articleList);
    }

    /**
     * @param ids 文章ids，多个英文逗号隔开
     * @return {@link Result }<{@link List }<{@link ArticleInfoVo }>>
     * @author Liuzixin
     * @date 2024年05月08日
     */
    @DeleteMapping("/{ids}")
    public Result<List<ArticleInfoVo>> deleteById(@PathVariable("ids") String ids){
        // ids按英文逗号隔开并转list
        List<String> idList = Arrays.asList(ids.split(","));
        return blogArticleService.deleteByIds(idList) ? Result.ok() : Result.fail();
    }

}
